package com.system.connect.springboot_flowers.service;

import com.alibaba.fastjson.JSON;
import com.system.connect.springboot_flowers.dto.FeedbackDTO;
import com.system.connect.springboot_flowers.entity.Feedback;
import com.system.connect.springboot_flowers.mapper.FeedbackMapper;
import com.system.connect.springboot_flowers.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class FeedbackService {
    
    @Autowired
    private FeedbackMapper feedbackMapper;
    
    public R submitFeedback(FeedbackDTO dto) {
        // 1. 参数验证
        if (StringUtils.isEmpty(dto.getTitle()) || 
            StringUtils.isEmpty(dto.getContent()) ||
            StringUtils.isEmpty(dto.getContact())) {
            return R.error("参数不完整");
        }
        
        // 2. 构建实体
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(dto, feedback);
        feedback.setImages(JSON.toJSONString(dto.getImages()));
        feedback.setStatus(0);
        
        // 3. 保存数据
        try {
            feedbackMapper.insert(feedback);
            return R.success("数据保存成功");
        } catch (Exception e) {
            log.error("保存反馈失败", e);
            return R.error("提交失败，请重试");
        }
    }
} 